package org.nmgyjt.springboot.beans.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.nimbusds.jose.JOSEException;
import lombok.extern.slf4j.Slf4j;
import org.nmgyjt.springboot.beans.admin.CsSystemUserCreateBean;
import org.nmgyjt.springboot.beans.vo.PeopleExcel;
import org.nmgyjt.springboot.controller.CsSystemUserController;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.admin.UserInfo;
import org.nmgyjt.springboot.service.ICsSystemUserService;
import org.nmgyjt.springboot.service.TypeService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class TypeDataListener extends AnalysisEventListener<PeopleExcel> {

    private ICsSystemUserService userService;
    private String createdBy;
    private CsSystemUserController csSystemUserController;

    public TypeDataListener(ICsSystemUserService userService,CsSystemUserController csSystemUserController,String createdBy) {
        this.userService = userService;
        this.csSystemUserController=csSystemUserController;
        this.createdBy=createdBy;
    }

    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 50;
    List<PeopleExcel> list = new ArrayList<>();

    @Override
    public void invoke(PeopleExcel data, AnalysisContext context) {
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始认证数据库！", list.size());
        if (!CollectionUtils.isEmpty(list)) {
            for(PeopleExcel data:list){
                CsSystemUserCreateBean bean=new CsSystemUserCreateBean();
                bean.setPhone(data.getPhone());
                try {
                    ApiRes apiRes=csSystemUserController.checkUser(bean);
                    log.info("验证返回数据：{}", apiRes);
                    if(!ObjectUtils.isEmpty(apiRes.getData())){
                        throw new RuntimeException(bean.getPhone()+"账号已存在，上传失败！");
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            log.info("{}条数据，开始存储数据库！", list.size());
            for(PeopleExcel data:list){
                CsSystemUserCreateBean bean=new CsSystemUserCreateBean();
                bean.setCreatedBy(createdBy);
                bean.setJob(data.getJob());
                bean.setMenuID("1000000010");
                bean.setName(data.getName());
                bean.setOrgName(data.getDep());
                bean.setPhone(data.getPhone());
                try {
                    UserInfo userInfo=userService.createCsSystemUser(bean);
                    log.info(userInfo.getId()+"存储数据库成功！");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
